package com.digital.herbarium.Controller;


import com.digital.herbarium.annotation.CheckToken;
import com.digital.herbarium.Entity.Dna;
import com.digital.herbarium.Entity.ResponseResult;
import com.digital.herbarium.annotation.Log;
import com.digital.herbarium.service.DnaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/dna")
public class DnaController {

    @Autowired
    DnaService dnaService;
    @Value("${static.excelurl}")
    private String excelurl;


    @GetMapping("getDnaInfo")
    @Log("dna")
    public ResponseResult getDnaInfo(Dna dna, @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                     @RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum){
        return ResponseResult.SUCCESS().setData( dnaService.createList(dna,pageSize,pageNum));
    }


    @GetMapping("getManagerDnaInfo")
    @CheckToken()
    @Log("dna")
    public ResponseResult getManagerDnaInfo(Dna dna,@RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                               @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum){
        return ResponseResult.SUCCESS().setData( dnaService.getDnaInfo(dna,pageSize,pageNum));
    }


    @PostMapping("addDnaInfo")
    @CheckToken()
    public  ResponseResult addDnaInfo(Dna dna){
        dnaService.addDnaInfo(dna);
        return ResponseResult.SUCCESS().setData("添加成功");
    }

    @PostMapping("editDnaInfo")
    @CheckToken()
    public  ResponseResult editDnaInfo(Dna dna){
        dnaService.editDnaInfo(dna);
        return ResponseResult.SUCCESS().setData("更新成功");
    }

    @GetMapping("delDnaInfo")
    @CheckToken()
    public ResponseResult delDnaInfo(String id){
        dnaService.delDnaInfo(id);
        return  ResponseResult.SUCCESS().setData("删除成功");
    }


    @PostMapping("uploadExcel")
    @CheckToken()
    public  ResponseResult uploadExcel(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
        dnaService.addExcel(file,request,excelurl);
        return ResponseResult.SUCCESS().setData("上传成功");
    }
}
